#' =============================================================================
#' FILE: 01_figure_01.R
#' DESCRIPTION:
#'   Creates Figure 1, visualizing partisanship and trust in political parties
#'   over time and by country in Latin America, using harmonized LAPOP and
#'   Latinobarometro datasets.
#'  For full replication, including the creation of the necessary dataframes, 
#'  please download the necessary data from:
#' [https://www.vanderbilt.edu/lapop/](https://www.vanderbilt.edu/lapop/)
#' [https://www.latinobarometro.org/lat.jsp](https://www.latinobarometro.org/lat.jsp)
#'
#' Details regarding the specific datasets can be found here:
#' 02_data/readme_lapop_data.txt
#' 02_data/readme_latinobarometro_data.txt
#'
#' PACKAGES REQUIRED: pacman, tidyverse, ggpubr
#'
#' OUTPUTS:
#'   - 03_figures/fg_01.jpg
#' =============================================================================

# Load Required Packages and data ----------------------------------------------

# Install and load required packages
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, ggpubr)

# Data
df_pid <- readRDS("04_outputs/lapop_partisanship.rds")
df_tru <- readRDS("04_outputs/latinobarometro_trust.rds")

# Partisanship over Time (Top-right panel) -------------------------------------
part_time <- bind_rows(
  # Dataset with observations for the region
  df_pid %>% group_by(year) %>% 
    summarise(partisanship = mean(partisanship), 
              country = "Latin America"),
  # Dataset with observations for Peru
  df_pid %>% 
    filter(pais == "Peru") %>% 
    group_by(year) %>% 
    summarise(partisanship = mean(partisanship), 
              country = "Peru")) %>% 
  mutate(country_label = ifelse(year == max(year), country, NA)) %>%
  ggplot(aes(x = year, 
             y = partisanship, 
             color = country, 
             label = country_label)) +
  geom_point() +
  geom_line() +
  geom_text(size = 3,
            nudge_y = 0.03) +
  scale_y_continuous(limits = c(0, 1)) +
  scale_color_manual(values = c("#A6B0BB", "#3A383F")) +
  scale_x_continuous(limits = c(2008, 2019), 
                     breaks = seq(2008, 2018, 2)) +
  labs(x = "Year", y = "Partisanship") +
  theme(legend.position = "none", 
        panel.background = element_rect(fill = 'white', colour = 'white'),
        panel.grid.major = element_line(colour = "grey95"),
        panel.grid.minor = element_line(colour = "grey95"),
        axis.ticks.y = element_blank(),
        axis.ticks.x = element_blank(),
        strip.background = element_rect(fill = "white"))
part_time

# Average Partisanship (Bottom-right Panel)-------------------------------------

# Set seed and iterations for bootstrapping
set.seed(1)
iterations <- 1000

# Bootstraps
mean_partisanship <- list()

for (i in 1:iterations) {
  # Sample with replacement
  df_a_boot <- df_pid[sample(nrow(df_pid), 
                             size = nrow(df_pid), 
                             replace = TRUE), ]
  
  # Estimate mean
  mean_partisanship[[i]] <- df_a_boot %>% group_by(pais) %>% 
    summarise(partisanship = mean(partisanship, na.rm = TRUE))
  print(i)
}

# To dataset
mean_partisanship <- do.call(rbind, mean_partisanship)

# Figure
part_ave <- mean_partisanship %>% 
  group_by(pais) %>% 
  summarise(quantile = c('lwr', 'mdn', 'upr'), 
            partisanship = quantile(partisanship, c(0.025, 0.5, 0.975))) %>% 
  pivot_wider(names_from = quantile, values_from = partisanship) %>% 
  mutate(peru = as.character(ifelse(pais == 'Peru', 1, 0))) %>%
  ggplot(aes(x = mdn, y = reorder(pais, mdn), color = peru)) +
  geom_point() +
  geom_errorbar(aes(xmin = lwr, xmax = upr, y = pais, width = 0.4)) +
  scale_color_manual(values = c("#A6B0BB", "#3A383F")) +
  labs(x = 'Partisanship (Average 2008-2018)', y = '') +
  scale_x_continuous(limits = c(0, 1)) +
  # geom_vline(xintercept = 0.2094737, color = "#A6B0BB", alpha = 0.5) +
  theme(legend.position = "none", 
        panel.background = element_rect(fill = 'white', colour = 'white'),
        panel.grid.major = element_line(colour = "grey95"),
        panel.grid.minor = element_line(colour = "grey95"),
        axis.ticks.y = element_blank(),
        axis.ticks.x = element_blank(),
        strip.background = element_rect(fill = "white"))
part_ave

# Trust over Time (Top-left panel) ---------------------------------------------

tru_time <- bind_rows(
  # Dataset with observations for the region
  df_tru %>%
    filter(country != "Spain") %>% 
    group_by(year) %>% 
    summarise(trust = mean(trust, na.rm = TRUE), 
              country = "Latin America"),
  # Dataset with observations for Peru
  df_tru %>% 
    filter(country == "Peru") %>% 
    group_by(year) %>% 
    summarise(trust = mean(trust, na.rm = TRUE), 
              country = "Peru")) %>% 
  mutate(country_label = ifelse(year == max(year), country, NA)) %>%
  ggplot(aes(x = year, 
             y = trust, 
             group = country, 
             color = country, 
             label = country_label)) +
  geom_line() +
  geom_point() +
  geom_text(size = 3,
            nudge_y = 0.015) +
  scale_color_manual(values = c("#A6B0BB", "#3A383F")) +
  labs(x = 'Year', y = 'Trust in Political Parties') +
  scale_x_continuous(limits = c(1999.98, 2021), breaks = c(2000, 2005, 2010, 2015, 2020), 
                     labels = scales::label_number(accuracy = 1, big.mark = '')) +
  scale_y_continuous(limits = c(0, .5)) +
  theme(legend.position = "none", 
        panel.background = element_rect(fill = 'white', colour = 'white'),
        panel.grid.major = element_line(colour = "grey95"),
        panel.grid.minor = element_line(colour = "grey95"),
        axis.ticks.y = element_blank(),
        axis.ticks.x = element_blank(),
        strip.background = element_rect(fill = "white"))
tru_time

# Average Trust (Bottom-left Panel) -------------------------------------------

# Eliminate Spain
df_tru <- df_tru %>% 
  filter(country != "Spain")

# Set seed and iterations
set.seed(1)
iterations <- 1000

# Loop
mean_trust <- list()

for (i in 1:iterations) {
  # Sample with replacement
  df_a_boot <- df_tru[sample(nrow(df_tru), 
                             size = nrow(df_tru), 
                             replace = TRUE), ]
  
  # Estimate mean
  mean_trust[[i]] <- df_a_boot %>% group_by(country) %>% 
    summarise(trust = mean(trust, na.rm = TRUE))
  print(i)
}

mean_trust <- do.call(rbind, mean_trust)

# Plot
tru_ave <- mean_trust %>% 
  group_by(country) %>% 
  summarise(quantile = c('lwr', 'mdn', 'upr'), 
            trust = quantile(trust, c(0.025, 0.5, 0.975))) %>% 
  pivot_wider(names_from = quantile, values_from = trust) %>% 
  mutate(peru = as.character(ifelse(country == 'Peru', 1, 0))) %>%
  ggplot(aes(x = mdn, y = reorder(country, mdn), color = peru)) +
  geom_point() +
  geom_errorbar(aes(xmin = lwr, xmax = upr, y = country, width = 0.4)) +
  scale_color_manual(values = c("#A6B0BB", "#3A383F")) +
  labs(x = 'Trust in Political Parties (Average 2002-2020)', y = '') +
  scale_x_continuous(limits = c(0, 0.5)) +
  geom_vline(xintercept = 0.2094737, color = "#A6B0BB", alpha = 0.5) +
  theme(legend.position = "none", 
        panel.background = element_rect(fill = 'white', colour = 'white'),
        panel.grid.major = element_line(colour = "grey95"),
        panel.grid.minor = element_line(colour = "grey95"),
        axis.ticks.y = element_blank(),
        axis.ticks.x = element_blank(),
        strip.background = element_rect(fill = "white"))
tru_ave

# Collect panels and save ------------------------------------------------------

# Collect
ggarrange(tru_time, 
          tru_ave, 
          part_time, 
          part_ave, 
          labels = c('a)', 'b)', 'c)', "d)"))

# Save
ggsave("03_figures/fg_01.jpg",  width = 18, height = 9)